package com.lagou.controller;


import com.lagou.edu.pojo.LagouToken;
import com.lagou.service.LagouEmailService;
import com.lagou.util.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/email")
public class EmailController {

    @Value("${mail.host}")
    private String mailHost;

    @Value("${mail.user}")
    private String mailUser;

    @Value("${mail.pwd}")
    private String mailPwd;

    @Value("${mail.from}")
    private String mailFromAddress;

    /**
     * 发送验证码到邮箱，true成功，false失败
     * @param email
     * @param code
     * @return
     */
    @RequestMapping(value = "/{email}/{code}")
    public boolean send(@PathVariable(value = "email") String email,
            @PathVariable(value = "code") String code) {
        MailUtils cn = new MailUtils();
        // 设置发件人地址、收件人地址和邮件标题
        cn.setAddress(mailFromAddress, email, "请查收验证码，用于激活账号", "本次申请的验证码为：" + code);
        //设置smtp服务器以及邮箱的帐号和密码
        try {
            cn.send(mailHost, mailUser, mailPwd);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
